package com.example.demo;

import org.apache.logging.log4j.util.PropertiesUtil;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @创建人 Mr.Yan
 * @创建时间 2018/9/3
 * @描述: 配置文件的写入和读取
 */
public class FileWrite {
    Properties prop;
    //读取到的配置文件内荣key value
    Map<String, String> PrMap;

    public FileWrite(String fileName) {
        prop = new Properties();
        PrMap = new HashMap<String, String>();
        this.read(fileName);
    }

    // 读取配置文件内容 放到Map 中
    public void read(String fileName) {
        //读取属性文件a.properties
        InputStream in = null;
        try {
            //不打jar包读取
            in = new BufferedInputStream(new FileInputStream(fileName));
            //in = getClass().getClassLoader().getResourceAsStream(fileName);
            //打jar包读取
            String filePath = System.getProperty("user.dir")+"/"+fileName;
            filePath = URLDecoder.decode(filePath,"utf-8");
            System.out.println("写入配置文件地址:_________________" + filePath);
            prop = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
           // prop.load(in);     ///加载属性列表
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = prop.getProperty(key);
                System.out.println("读取配置文件:_________________"+key + ":" + prop.getProperty(key));
                PrMap.put(key,value);
            }
            //in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //写入配置文件，fileName 文件名称 ，keyValueMap 写入的map 会清空 老的文件 ， 先获取老文件的Map
    public void write(String fileName,Map<String,String> keyValueMap){
        //不打jar包写入
       String filePath ;//= PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
        //打jar包写入
        filePath = System.getProperty("user.dir")+"/"+fileName;
        //filePath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile()+"BOOT_INF/classess/"+fileName;
        System.out.println("地址——————————————"+filePath);
        BufferedWriter bw = null;
        OutputStream os =null;
        Properties props = null;
        try {
            filePath = URLDecoder.decode(filePath,"utf-8");
            System.out.println("写入配置文件地址:_________________" + filePath);
            props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
            System.out.println("写入配置文件:_________________:"+props);

            // 写入属性文件
            //bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
            os = new FileOutputStream(filePath);
            System.out.println("清空开始:_________________:"+props);
            props.clear();// 清空旧的文件
            System.out.println("清空完成:_________________:"+props);
            for (String key : keyValueMap.keySet())
                props.setProperty(key, keyValueMap.get(key));

            System.out.println("写入后的配置文件:_________________"+props);
            props.store(os, "");

        } catch (IOException e) {
            System.out.println("报错了:_________________:");
            System.out.println(e.getMessage() + "写入机器码失败————————————————————");
        } finally {
            try {
                System.out.println("写入结束:_________________:");
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String [] args){
        //读取
        FileWrite fil =  new FileWrite("register.properties");
        System.out.println(fil.PrMap);
        /*Map<String,String> map = fil.PrMap;
        //写入
        map.put("MKEY","aa234");
        fil.write("register.properties",map);*/
    }
}
